package com.example.demo.job;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class ScheduledProcessor implements ApplicationContextAware {
    private ApplicationContext context;
    private ScheduledAnnotationBeanPostProcessor processor;

    @PostConstruct
    public void initProcessor() {
//            processor = (ScheduledAnnProcesser) context.getBean("scheduledAnnProcesser");
        processor = (ScheduledAnnotationBeanPostProcessor) context.getBean("org.springframework.context.annotation.internalScheduledAnnotationProcessor");
    }

    public void stopTask(Object bean) {
        processor.postProcessBeforeDestruction(bean, null);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    public Object getBean(Class<?> str) {
        return context.getBean(str);
    }
}